﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;
namespace Web
{
    public partial class CheckSalesHistory : System.Web.UI.Page
    {
        QueryController queryController=new QueryController();
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                IList<SalesHistoryEntity> salesHistoryEntities = queryController.findAllSalesHistory();
                SalesHistoryGridView.DataSource = salesHistoryEntities;
                SalesHistoryGridView.DataBind();
            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }
        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }
        protected void SalesHistoryGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            SalesHistoryGridView.PageIndex = e.NewPageIndex;
            GetResultEntitiesByConditions();
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            GetResultEntitiesByConditions();
        }

        protected void GetResultEntitiesByConditions()
        {
            string poNo = SearchPONoTextBox.Text.Trim().ToUpper();
            string productNo = SearchProductNoTextBox.Text.Trim().ToUpper();

            DateTime from = (FromTextBox.Text.Length>0) ? Convert.ToDateTime(FromTextBox.Text):DateTime.MinValue;
            DateTime to = (ToTextBox.Text.Length>0)?Convert.ToDateTime(ToTextBox.Text):DateTime.MinValue;

            IList<SalesHistoryEntity> salesHistoryEntities = queryController.FindSalesHistoryByConditions(poNo, productNo, from, to);
            SalesHistoryGridView.DataSource = salesHistoryEntities;
            SalesHistoryGridView.DataBind();
        }
    }
}